package com.mingteng.mobile_feedback.controller;

import com.alibaba.fastjson.JSON;
import com.mingteng.mobile_feedback.domin.*;
import com.mingteng.mobile_feedback.respository.EvaluteResultsRepository;
import com.mingteng.mobile_feedback.respository.JdbcRepository;
import com.mingteng.mobile_feedback.respository.OrderEvaluteRepository;
import com.mingteng.mobile_feedback.respository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping(value = "/mobile")
public class MobileController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private EvaluteResultsRepository evaluteResultsRepository;

    @Autowired
    private OrderEvaluteRepository orderEvaluteRepository;

    @Autowired
    private JdbcRepository jdbcRepository;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String Login(String code,String password){
        Map<String,Object> retData = new HashMap<>();

        String passwordMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        UserInfo user = userRepository.findFirstByUserCodeAndPassword(code,passwordMd5);

        retData.put("user",user);

        return JSON.toJSONString(retData);
    }

    @RequestMapping(value = "/planList",method = RequestMethod.POST)
    public String getPlanList(String userCode){
        Map<String,Object> retData = new HashMap<>();

        List<PlanInfo> list = jdbcRepository.getPlanList(userCode);
        retData.put("list",list);
        return JSON.toJSONString(retData);
    }

    @RequestMapping(value = "/lastOrders",method = RequestMethod.POST)
    public String getLastOrders(String orderId,String gxName){
        Map<String,Object> retData = new HashMap<>();

        List<ViewMesScInfoList> list = jdbcRepository.getViewScInfoList(orderId, gxName);
        retData.put("list",list);
        return JSON.toJSONString(retData);
    }

    @RequestMapping(value = "/evalLastOrder",method = RequestMethod.POST)
    public String evalLastOrder(String scCode,String orderName){
        Map<String,Object> retData = new HashMap<>();

        List<OrderEvalute> list = orderEvaluteRepository.findByScOrderCodeAndGxNameAndLastOrderCode("SC201912030005","GX004","GX001");

        retData.put("list",list);
        return JSON.toJSONString(retData);
    }

    @RequestMapping(value = "/bqParamLists",method = RequestMethod.POST)
    public String getbqParamLists(String scCode,String orderName){
        Map<String,Object> retData = new HashMap<>();
        List<EvaluteResults> list = evaluteResultsRepository.findByScOrderCodeAndGxCodeOrderByEvalResult(scCode,orderName);

        retData.put("list",list);
        return JSON.toJSONString(retData);
    }


    @RequestMapping(value = "/saveQuality",method = RequestMethod.POST)
    public String SaveQuality(String selfEval,String evalResult,String note,String submitUserCode,String notType,String selfSpRe,String id){
        Map<String,Object> retData = new HashMap<>();

        if(evalResult.equals("不合格") && (id == null || id.equals("") )){
            EvaluteResults evaluteResults = evaluteResultsRepository.findById(id).get();
            evaluteResults.setSelfEval(selfEval);
            evaluteResults.setEvalResult(evalResult);
            evaluteResults.setJudgeProgress("评审中");
            evaluteResults.setNote(note);
            evaluteResults.setNextJudgeOrder("1");
            evaluteResults.setSubmitUserCode(submitUserCode);
            evaluteResults.setNotType(notType);
            evaluteResults.setSelfSpRe(selfSpRe);

            evaluteResultsRepository.save(evaluteResults);

            jdbcRepository.insertJudgeInfo(id);
        }else{
            if(id != null)
            {
                EvaluteResults evaluteResults = evaluteResultsRepository.findById(id).get();
                evaluteResults.setSelfEval(selfEval);
                evaluteResults.setEvalResult(evalResult);
                evaluteResults.setJudgeProgress("完结");
                evaluteResults.setNote(note);
                evaluteResults.setNextJudgeOrder("-1");
                evaluteResults.setSubmitUserCode(submitUserCode);
                evaluteResults.setNotType(notType);
                evaluteResults.setSelfSpRe(selfSpRe);

                evaluteResultsRepository.save(evaluteResults);

                if(id.equals("报废")){
                    jdbcRepository.updateScOrder(id);
                }
                if(id.equals("返修")){
                    EvaluteResults evaluteResults2 = evaluteResultsRepository.findById(id).get();
                    EvaluteResults evaluteResults1 = new EvaluteResults();

                    evaluteResults1.setNotType(evaluteResults2.getNotType());
                    evaluteResults1.setJudgeProgress(evaluteResults2.getJudgeProgress());
                    evaluteResults1.setEvalResult(evaluteResults2.getEvalResult());
                    evaluteResults1.setSelfEval(evaluteResults2.getSelfEval());
                    evaluteResults1.setCustomerName(evaluteResults2.getCustomerName());
                    evaluteResults1.setGxCode(evaluteResults2.getGxCode());
                    evaluteResults1.setGxName(evaluteResults2.getGxName());
                    evaluteResults1.setJudgeLevel(evaluteResults2.getJudgeLevel());
                    evaluteResults1.setMaxValue(evaluteResults2.getMaxValue());
                    evaluteResults1.setMinValue(evaluteResults2.getMinValue());
                    evaluteResults1.setParamName(evaluteResults2.getParamName());
                    evaluteResults1.setProductName(evaluteResults2.getProductName());
                    evaluteResults1.setParamType(evaluteResults2.getParamType());
                    evaluteResults1.setQualitativeDescribe(evaluteResults2.getQualitativeDescribe());
                    evaluteResults1.setScOrderCode(evaluteResults2.getScOrderCode());
                    evaluteResults1.setSn(evaluteResults2.getSn());
                    evaluteResults1.setStandard(evaluteResults2.getStandard());
                    evaluteResults1.setTestStandard(evaluteResults2.getTestStandard());

                    evaluteResultsRepository.save(evaluteResults1);
                }
            }
        }

        return JSON.toJSONString(retData);
    }

    @RequestMapping(value = "/saveLastOrder",method = RequestMethod.POST)
    public String saveLastOrder(String scOrderCode,String gxName){
        Map<String,Object> retData = new HashMap<>();

//        EvaluteResults eva = evaluteResultsRepository.findByScOrderCodeAndGxName(scOrderCode, gxName);
//
//        if(eva != null)
//        {
//            OrderEvalute oe = new OrderEvalute();
//            oe.setScOrderCode(eva.getScOrderCode());
//            oe.setCustomerName(eva.getCustomerName());
//            oe.setProductName(eva.getProductName());
//            oe.setGxCode(eva.getGxCode());
//            oe.setGxName(eva.getGxName());
//            oe.setLastOrderCode();
//            oe.setLastOrderName();
//            oe.setEvalResult();
//            oe.setNote();
//
//            try {
//                orderEvaluteRepository.save(oe);
//            }catch (Exception e)
//            {
//                retData.put("result",false);
//            }
//
//            retData.put("result",true);
//        }

        retData.put("result",true);
        return JSON.toJSONString(retData);
    }
}
